package com.example.exception;

/**
 * 系统异常类
 * 用于处理系统级别的异常
 */
public class SystemException extends BaseException {
    
    public SystemException(String errorCode, String errorMessage) {
        super(errorCode, errorMessage, ErrorLevel.ERROR, ErrorCategory.SYSTEM);
    }
    
    public SystemException(String errorCode, String errorMessage, Throwable cause) {
        super(errorCode, errorMessage, cause);
        setErrorLevel(ErrorLevel.ERROR);
        setErrorCategory(ErrorCategory.SYSTEM);
    }
    
    public SystemException(String errorCode, String errorMessage, ErrorLevel errorLevel) {
        super(errorCode, errorMessage, errorLevel, ErrorCategory.SYSTEM);
    }
    
    // ========== 数据库相关系统异常 ==========
    
    public static SystemException databaseConnectionError() {
        return new SystemException(ErrorCode.DATABASE_CONNECTION_ERROR, "数据库连接失败");
    }
    
    public static SystemException databaseConnectionError(Throwable cause) {
        return new SystemException(ErrorCode.DATABASE_CONNECTION_ERROR, "数据库连接失败", cause);
    }
    
    public static SystemException databaseQueryError() {
        return new SystemException(ErrorCode.DATABASE_QUERY_ERROR, "数据库查询失败");
    }
    
    public static SystemException databaseQueryError(Throwable cause) {
        return new SystemException(ErrorCode.DATABASE_QUERY_ERROR, "数据库查询失败", cause);
    }
    
    public static SystemException databaseUpdateError() {
        return new SystemException(ErrorCode.DATABASE_UPDATE_ERROR, "数据库更新失败");
    }
    
    public static SystemException databaseUpdateError(Throwable cause) {
        return new SystemException(ErrorCode.DATABASE_UPDATE_ERROR, "数据库更新失败", cause);
    }
    
    public static SystemException databaseTransactionError() {
        return new SystemException(ErrorCode.DATABASE_TRANSACTION_ERROR, "数据库事务失败");
    }
    
    public static SystemException databaseTransactionError(Throwable cause) {
        return new SystemException(ErrorCode.DATABASE_TRANSACTION_ERROR, "数据库事务失败", cause);
    }
    
    // ========== 缓存相关系统异常 ==========
    
    public static SystemException cacheError() {
        return new SystemException(ErrorCode.CACHE_ERROR, "缓存操作失败");
    }
    
    public static SystemException cacheError(Throwable cause) {
        return new SystemException(ErrorCode.CACHE_ERROR, "缓存操作失败", cause);
    }
    
    public static SystemException cacheKeyNotFound() {
        return new SystemException(ErrorCode.CACHE_KEY_NOT_FOUND, "缓存键不存在");
    }
    
    // ========== 外部服务相关系统异常 ==========
    
    public static SystemException externalServiceUnavailable() {
        return new SystemException(ErrorCode.EXTERNAL_SERVICE_UNAVAILABLE, "外部服务不可用");
    }
    
    public static SystemException externalServiceUnavailable(String serviceName) {
        return new SystemException(ErrorCode.EXTERNAL_SERVICE_UNAVAILABLE, "外部服务不可用: " + serviceName);
    }
    
    public static SystemException externalServiceTimeout() {
        return new SystemException(ErrorCode.EXTERNAL_SERVICE_TIMEOUT, "外部服务超时");
    }
    
    public static SystemException externalServiceTimeout(String serviceName) {
        return new SystemException(ErrorCode.EXTERNAL_SERVICE_TIMEOUT, "外部服务超时: " + serviceName);
    }
    
    public static SystemException externalServiceError() {
        return new SystemException(ErrorCode.EXTERNAL_SERVICE_ERROR, "外部服务错误");
    }
    
    public static SystemException externalServiceError(String serviceName, Throwable cause) {
        return new SystemException(ErrorCode.EXTERNAL_SERVICE_ERROR, "外部服务错误: " + serviceName, cause);
    }
    
    // ========== 网络相关系统异常 ==========
    
    public static SystemException networkError() {
        return new SystemException(ErrorCode.NETWORK_ERROR, "网络连接失败");
    }
    
    public static SystemException networkError(Throwable cause) {
        return new SystemException(ErrorCode.NETWORK_ERROR, "网络连接失败", cause);
    }
    
    public static SystemException timeoutError() {
        return new SystemException(ErrorCode.TIMEOUT_ERROR, "请求超时");
    }
    
    public static SystemException timeoutError(String operation) {
        return new SystemException(ErrorCode.TIMEOUT_ERROR, "请求超时: " + operation);
    }
    
    // ========== 配置相关系统异常 ==========
    
    public static SystemException configNotFound() {
        return new SystemException(ErrorCode.CONFIG_NOT_FOUND, "配置不存在");
    }
    
    public static SystemException configNotFound(String configKey) {
        return new SystemException(ErrorCode.CONFIG_NOT_FOUND, "配置不存在: " + configKey);
    }
    
    public static SystemException configParseError() {
        return new SystemException(ErrorCode.CONFIG_PARSE_ERROR, "配置解析失败");
    }
    
    public static SystemException configParseError(String configKey, Throwable cause) {
        return new SystemException(ErrorCode.CONFIG_PARSE_ERROR, "配置解析失败: " + configKey, cause);
    }
    
    // ========== 通用系统异常 ==========
    
    public static SystemException systemError() {
        return new SystemException(ErrorCode.SYSTEM_ERROR, "系统内部错误");
    }
    
    public static SystemException systemError(String message) {
        return new SystemException(ErrorCode.SYSTEM_ERROR, "系统内部错误: " + message);
    }
    
    public static SystemException systemError(Throwable cause) {
        return new SystemException(ErrorCode.SYSTEM_ERROR, "系统内部错误", cause);
    }
    
    public static SystemException systemError(String message, Throwable cause) {
        return new SystemException(ErrorCode.SYSTEM_ERROR, "系统内部错误: " + message, cause);
    }
    
    public static SystemException unknownError() {
        return new SystemException(ErrorCode.UNKNOWN_ERROR, "未知错误");
    }
    
    public static SystemException unknownError(Throwable cause) {
        return new SystemException(ErrorCode.UNKNOWN_ERROR, "未知错误", cause);
    }
}
